/*
 * @Author: jh
 * 数组添加、去重
 * 数组的tag删除
 */
import {api} from '__views/system-settings/user/api';
import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
export default {
    data() {
        return {
        }
    },
    methods: {
        /*数组的tag删除
        * tag 标签对象
        * checkedList 选中的下拉项
        * */
        handleCloseTag(tag,checkedList,type,callback ) {
            let str = ''
            if (type === '6') {
                str = '客户类型'
            } else {
                str = '行业'
            }
            let tip = '是否删除此' + str + '?'
            this.$confirm(tip, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let index = checkedList.indexOf(tag)
                checkedList.splice(index, 1);
                //刷新dom
                checkedList = checkedList.filter(function (item) {
                    return item
                })
                callback() //令所选的下拉框中选中的项清空
            },() => {
                // this.$message({
                //     type: 'info',
                //     message: '已取消删除',
                //     center: true
                // });
            })
        },
        /* 数组添加、去重
         * id 选中的项的value
         * checkedList 选中的下拉项列表
         * allList 所有的下拉项列表
         * color 选中的变色 boolean
         * callBack
         * */
        addToArr(id,checkedList,allList,color,callBack) {
            let flag = false;
            for (let item of checkedList) {
                if (item.id === id) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                for (let item of allList) {
                    if (item.id === id) {
                        //让选中的置灰
                        //item.disabled = true
                        // checkedList.splice(0, 0, item);
                        // if (callBack2) {
                        //     callBack2()
                        // }
                        checkedList.push(item);
                    }
                }
            }
            //设置颜色
            if (color) {
                let curentTagIndex = 0
                checkedList.map((tag,index)=>{
                    if (id === tag.id) {
                        tag.color = 'danger';
                        curentTagIndex = index  //当前下标
                        if (callBack) {
                            callBack(id,curentTagIndex)
                        }
                    } else {
                        tag.color = '';
                    }
                })
            }
            //this.searchStaff=''
        },
        //修改数据权限
        editDataAuth(userId,roleId,type,dataId,callBack) {
            dataId = dataId.join(',');
            let params = {
                userId: userId,
                roleId: roleId,
                type: type,
                dataId: dataId,
            }
            //console.log(params,'修改数据权限canshu')
            api.EDIT_DATA_AUTH.url = '/userEntity/users/dataPower?userId=' + params.userId + '&roleId=' + params.roleId + '&type=' + params.type + '&dataId=' + params.dataId
                apiHttp(api.EDIT_DATA_AUTH,params).then(ret=> {
                if (ret.resCode === STATUS_HTTP_SUCCESS) {
                    this.$message({
                        message: '数据权限修改成功',
                        type: 'success'
                    });
                    if (callBack) {
                        callBack()
                    }
                }
            })
        }
    }
}
